package bandi.osgi.dal.dao.devicetypes.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import bandi.osgi.dal.dao.abstr.AbstractDAO;
import bandi.osgi.dal.dao.beans.DeviceType;
import bandi.osgi.dal.dao.devicetypes.DeviceTypeDAO;

public class DeviceTypeDAOImpl extends AbstractDAO implements DeviceTypeDAO {

	@Override
	public void deleteDeviceType(DeviceType deviceType) {
		final String sql = "DELETE FROM DEVICE_TYPES WHERE DEVICE_TYPE_ID = "
				+ deviceType.getDevice_type_id();
		Connection connection = null;
		Statement statement = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			statement.executeUpdate(sql);
			statement.close();
		} catch (final Exception e) {
			System.err.println("Internal error: " + e.getMessage());
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public DeviceType getDeviceType(long deviceTypeID) {
		final String sql = "SELECT * FROM DEVICE_TYPES WHERE DEVICE_TYPE_ID = "
				+ deviceTypeID;
		Connection connection = null;
		Statement statement = null;
		DeviceType dt = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			final ResultSet rs = statement.executeQuery(sql);
			rs.next();
			dt = new DeviceType(rs.getLong("DEVICE_TYPE_ID"), rs
					.getString("FIRMWARE"), rs.getString("FIRMWARE_VERSION"),
					rs.getString("MANUFACTURER"), rs.getString("MODEL"), rs
							.getString("OS"), rs.getString("OS_VERSION"), rs
							.getString("TYPE"));
			statement.close();
			return dt;
		} catch (final Exception e) {
			System.err.println("Internal error :" + e.getMessage());
			return null;
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public List<DeviceType> getDeviceTypes() {
		final String sql = "SELECT * FROM DEVICE_TYPES";
		Connection connection = null;
		Statement statement = null;
		final List<DeviceType> list = new ArrayList<DeviceType>();
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			final ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				list.add(new DeviceType(rs.getLong("DEVICE_TYPE_ID"), rs
						.getString("FIRMWARE"), rs
						.getString("FIRMWARE_VERSION"), rs
						.getString("MANUFACTURER"), rs.getString("MODEL"), rs
						.getString("OS"), rs.getString("OS_VERSION"), rs
						.getString("TYPE")));
			}
			statement.close();
			return list;
		} catch (final Exception e) {
			System.err.println("Internal error: " + e.getMessage());
			return null;
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public List<DeviceType> getDeviceTypes(String manufacturer) {
		final String sql = "SELECT * FROM DEVICE_TYPES WHERE MANUFACTURER = "
				+ manufacturer;
		Connection connection = null;
		Statement statement = null;
		final List<DeviceType> list = new ArrayList<DeviceType>();
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			final ResultSet rs = statement.executeQuery(sql);
			while (rs.next()) {
				list.add(new DeviceType(rs.getLong("DEVICE_TYPE_ID"), rs
						.getString("FIRMWARE"), rs
						.getString("FIRMWARE_VERSION"), rs
						.getString("MANUFACTURER"), rs.getString("MODEL"), rs
						.getString("OS"), rs.getString("OS_VERSION"), rs
						.getString("TYPE")));
			}
			statement.close();
			return list;
		} catch (final Exception e) {
			System.err.println("Internal error: " + e.getMessage());
			return null;
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

	@Override
	public void insertDeviceType(DeviceType deviceType) {
		final String sql = "INSERT INTO DEVICE_TYPES(DEVICE_TYPE_ID, TYPE, MANUFACTURER, MODEL, OS, FIRMWARE, OS_VERSION, FIRMWARE_VERSION) "
				+ "VALUES("
				+ null
				+ ", '"
				+ deviceType.getType()
				+ "', '"
				+ deviceType.getManufacturer()
				+ "', '"
				+ deviceType.getModel()
				+ "', '"
				+ deviceType.getOs()
				+ "', '"
				+ deviceType.getFirmware()
				+ "', '"
				+ deviceType.getOs_version()
				+ "', '"
				+ deviceType.getFirmware_version() + "')";
		Connection connection = null;
		Statement statement = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			statement.executeUpdate(sql);
			statement.close();
		} catch (final Throwable e) {
			System.err.println("Internal error: " + e.getMessage());
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}

	}

	@Override
	public void updateDeviceType(DeviceType deviceType) {
		final String sql = "UPDATE DEVICE_TYPES SET TYPE = '"
				+ deviceType.getType() + "', MANUFACTURER = '"
				+ deviceType.getManufacturer() + "', MODEL = '"
				+ deviceType.getModel() + "', OS = '" + deviceType.getOs()
				+ "', FIRMWARE = '" + deviceType.getFirmware()
				+ "', OS_VERSION = '" + deviceType.getOs_version()
				+ "', FIRMWARE_VERSION = '" + deviceType.getFirmware_version()
				+ "' WHERE DEVICE_TYPE_ID = " + deviceType.getDevice_type_id();
		Connection connection = null;
		Statement statement = null;
		try {
			connection = super.createConnection();
			statement = connection.createStatement();
			statement.executeUpdate(sql);
			statement.close();
		} catch (final Throwable e) {
			System.err.println("Internal error: " + e.getMessage());
		} finally {
			if (statement != null) {
				try {
					statement.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
			if (connection != null) {
				try {
					connection.close();
				} catch (final SQLException ignored) {
					// Ignoring this exception, as we can't do much.
				}
			}
		}
	}

}
